﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELS.User;

namespace ELS.ReportStatistic
{
    public partial class ReportStatistic : Form
    {
        private TextBox txtStatus;
        private PictureBox pbStatus;
        private Panel pnlReportStatistic;
        private ReportStatisticController reportController = new ReportStatisticController();

        public ReportStatistic()
        {
            InitializeComponent();
        }

        public ReportStatistic(TextBox txtStatus, PictureBox pbStatus, Panel pnlReportStatistic)
        {
            InitializeComponent();
            this.txtStatus = txtStatus;
            this.pbStatus = pbStatus;
            this.pnlReportStatistic = pnlReportStatistic;
            LoadDataGridReport();
        }

        private void LoadDataGridReport()
        {            
            drgReportStatistic.DataSource = reportController.GetAllDataReport();
        }
        private ReportViewer frm_Viewer;
        private PieChartReport rpt_PieChartReport;
        private ColumnChartReport rpt_ColumnChartReport;
        private LineChartReport rpt_LineChartReport;
        private AreaChartReport rpt_AreaChartReport;

        private void btn_Print_Click(object sender, EventArgs e)
        {
            SaveChanges();
            #region Ex1
            frm_Viewer = new ReportViewer();
            DataTable dtSalary = new DataTable();
            dtSalary = reportController.GetAllDataReport();

            if (rbtnPie.Checked)
            {
                rpt_PieChartReport = new PieChartReport();
                rpt_PieChartReport.SetDataSource(dtSalary);
                rpt_PieChartReport.SetParameterValue("StringParameter", "================");
                frm_Viewer.crv_Viewer.ReportSource = rpt_PieChartReport;
            }
            if (rbtnLine.Checked)
            {
                rpt_LineChartReport = new LineChartReport();
                rpt_LineChartReport.SetDataSource(dtSalary);
                rpt_LineChartReport.SetParameterValue("StringParameter", "================");
                frm_Viewer.crv_Viewer.ReportSource = rpt_LineChartReport;
            }
            if (rbtnColumn.Checked)
            {
                rpt_ColumnChartReport = new ColumnChartReport();
                rpt_ColumnChartReport.SetDataSource(dtSalary);
                rpt_ColumnChartReport.SetParameterValue("StringParameter", "================");
                frm_Viewer.crv_Viewer.ReportSource = rpt_ColumnChartReport;
            }
            if (rbtnArea.Checked)
            {
                rpt_AreaChartReport = new AreaChartReport();
                rpt_AreaChartReport.SetDataSource(dtSalary);
                rpt_AreaChartReport.SetParameterValue("StringParameter", "================");
                frm_Viewer.crv_Viewer.ReportSource = rpt_AreaChartReport;
            }            
            frm_Viewer.ShowDialog();
            #endregion
        }

        private void SaveChanges()
        {
            string strQuery = string.Empty;
            DataTable dtChanges;
            //
            // Get the Updated DataTable back from the DataGridView
            DataTable dtEmployee = (DataTable)drgReportStatistic.DataSource;
            //
            
            // Get the Modified Rows by filtering on their RowState
            dtChanges = dtEmployee.GetChanges(DataRowState.Modified);

            if (dtChanges != null)
            {
                //
                // Form the UPDATE Query to Update the Rows.
                reportController.UpdateToRow(dtChanges);
                //if (reportController.UpdateToRow(dtChanges))
                    //UserCommon.NotifyOnMainForm(txtStatus, pbStatus, "Update OK", 1);
                //else
                    //UserCommon.NotifyOnMainForm(txtStatus, pbStatus, "Update Fail", -1);
                dtChanges = null;
            }
            LoadDataGridReport();
        }
    }
}
